#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

typedef struct sockaddr SA;

int main(int argc, char *argv[])
{
    int                 sockfd;
    socklen_t           len;
    struct sockaddr_un  addr1, addr2;

    if (argc != 2) {
        fprintf(stderr, "usage: %s <pathname>\n", argv[0]);
        exit(1);
    }

    sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
    memset(&addr1, 0, sizeof(addr1));
    addr1.sun_family = AF_LOCAL;
    strncpy(addr1.sun_path, argv[1], sizeof(addr1.sun_path)-1);

    if (bind(sockfd, (SA *)&addr1, SUN_LEN(&addr1)) < 0) {
        perror("bind error");
        exit(1);
    }

    len = sizeof(addr2);
    if (getsockname(sockfd, (SA *)&addr2, &len) < 0) {
        perror("getsockname error");
        exit(1);
    }

    printf("bound name = %s, returned len = %d\n", addr2.sun_path, len);

    unlink(argv[1]);

    return 0;
}
